package com.demo.demo2;

import com.demo.demo2.beans.ChannelVO;
import com.demo.demo2.beans.CityChannelVO;
import com.demo.demo2.beans.ExportCityChannelVO;
import com.demo.demo2.beans.StoreChannelVO;
import com.demo.util.FileUtil;
import com.demo.util.excel.beans.ExcelColumnVO;
import com.demo.util.excel.builders.ExcelColumnsBuilder;
import com.demo.util.excel.builders.ExcelStyleBuilder;
import com.demo.util.excel.builders.ExportExcelBuilder;
import lombok.Cleanup;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Demo2 {

    public static void main(String[] args) throws IOException {

        ExcelStyleBuilder excelStyle = new ExcelStyleBuilder()
                .add("headerStyle", Demo2::getHeaderStyle)
                .add("cellStyle", Demo2::getCellStyle)
                .add("cityCellStyle", Demo2::getCityCellStyle)
                .add("storeCellStyle", Demo2::getStoreCellStyle)
                .add("rateCellStyle", Demo2::getRateCellStyle)
                .add("lastRowCellStyle", Demo2::getLastRowCellStyle)
        ;

        List<CityChannelVO> data = getExportData();

        List<ExportCityChannelVO> exportData = new ArrayList<>();
        for (CityChannelVO info : data) {
            for (StoreChannelVO store : info.getStores()) {
                exportData.add(new ExportCityChannelVO(info, store));
            }
        }

        List<ExcelColumnVO<ExportCityChannelVO>> columns = new ExcelColumnsBuilder<ExportCityChannelVO>()
                .add(CityChannelVO::getCity).setMerge(ExportCityChannelVO::cityEquals)
                .add(StoreChannelVO::getStoreName)
                .add(CityChannelVO::getTargetCount).setMerge(ExportCityChannelVO::cityEquals)
                .add(CityChannelVO::getAchievedCount).setMerge(ExportCityChannelVO::cityEquals)
                .add(CityChannelVO::getUnusedCount).setMerge(ExportCityChannelVO::cityEquals)
                .add(CityChannelVO::getAchievedRate).setMerge(ExportCityChannelVO::cityEquals)
                .add(StoreChannelVO::getTargetCount)
                .add("第5周目标", "storeInfo.channel.4.targetCount")
                .add("第5周达成数", "storeInfo.channel.4.achievedCount")
                .add("第5周退租数", "storeInfo.channel.4.unusedCount")
                .add("第5周达成率", "storeInfo.channel.4.achievedRate")
                .add("第4周目标", "storeInfo.channel.3.targetCount")
                .add("第4周达成数", "storeInfo.channel.3.achievedCount")
                .add("第4周退租数", "storeInfo.channel.3.unusedCount")
                .add("第4周达成率", "storeInfo.channel.3.achievedRate")
                .add("第3周目标", "storeInfo.channel.2.targetCount")
                .add("第3周达成数", "storeInfo.channel.2.achievedCount")
                .add("第3周退租数", "storeInfo.channel.2.unusedCount")
                .add("第3周达成率", "storeInfo.channel.2.achievedRate")
                .add("第2周目标", "storeInfo.channel.1.targetCount")
                .add("第2周达成数", "storeInfo.channel.1.achievedCount")
                .add("第2周退租数", "storeInfo.channel.1.unusedCount")
                .add("第2周达成率", "storeInfo.channel.1.achievedRate")
                .add("第1周目标", "storeInfo.channel.0.targetCount")
                .add("第1周达成数", "storeInfo.channel.0.achievedCount")
                .add("第1周退租数", "storeInfo.channel.0.unusedCount")
                .add("第1周达成率", "storeInfo.channel.0.achievedRate")
                .done();

        @Cleanup
        XSSFWorkbook workbook = new XSSFWorkbook();
        new ExportExcelBuilder<ExportCityChannelVO>(workbook)
                .addStyle(excelStyle)
                .addSheet("Sheet 1", ExportCityChannelVO::getRowCellStyle, columns, exportData)
                .save("D:\\Downloads\\test2.xlsx");

        FileUtil.openFile("D:\\Downloads\\test2.xlsx");
    }


    public static XSSFCellStyle getHeaderStyle(@NotNull XSSFWorkbook workbook) {
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        setStyleBolder(cellStyle);
        setStyleCenter(cellStyle);
        XSSFFont font = workbook.createFont();
        font.setColor(IndexedColors.BLACK.getIndex());
        font.setBold(true);
        font.setFontHeight(14);
        cellStyle.setFont(font);
        cellStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(244,247,253)));
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return cellStyle;
    }

    public static XSSFCellStyle getCellStyle(@NotNull XSSFWorkbook workbook) {
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        setStyleBolder(cellStyle);
        setStyleCenter(cellStyle);
        XSSFFont font = workbook.createFont();
        font.setColor(IndexedColors.BLACK.getIndex());
        font.setFontHeight(12);
        cellStyle.setFont(font);
        cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return cellStyle;
    }

    public static XSSFCellStyle getCityCellStyle(@NotNull XSSFWorkbook workbook) {
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        setStyleBolder(cellStyle);
        setStyleCenter(cellStyle);
        XSSFFont font = workbook.createFont();
        font.setColor(new XSSFColor(new java.awt.Color(72, 122, 229)));
        font.setFontHeight(12);
        cellStyle.setFont(font);
        cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return cellStyle;
    }

    public static XSSFCellStyle getStoreCellStyle(@NotNull XSSFWorkbook workbook) {
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        setStyleBolder(cellStyle);
        setStyleCenter(cellStyle);
        XSSFFont font = workbook.createFont();
        font.setColor(new XSSFColor(new java.awt.Color(190,190,190)));
        font.setFontHeight(12);
        cellStyle.setFont(font);
        cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return cellStyle;
    }

    public static XSSFCellStyle getRateCellStyle(@NotNull XSSFWorkbook workbook) {
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        setStyleBolder(cellStyle);
        setStyleCenter(cellStyle);
        XSSFFont font = workbook.createFont();
        font.setColor(new XSSFColor(new java.awt.Color(142,190,121)));
        font.setFontHeight(12);
        cellStyle.setFont(font);
        cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return cellStyle;
    }

    public static XSSFCellStyle getLastRowCellStyle(@NotNull XSSFWorkbook workbook) {
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        setStyleBolder(cellStyle);
        setStyleCenter(cellStyle);
        XSSFFont font = workbook.createFont();
        font.setColor(new XSSFColor(new java.awt.Color(247,182,34)));
        font.setFontHeight(12);
        cellStyle.setFont(font);
        cellStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(254,250,244)));
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return cellStyle;
    }

    public static void setStyleCenter(@NotNull XSSFCellStyle cellStyle) {
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    }

    public static void setStyleBolder(@NotNull XSSFCellStyle cellStyle) {
        cellStyle.setBorderTop(BorderStyle.DASHED);
        cellStyle.setBorderBottom(BorderStyle.DASHED);
        cellStyle.setBorderRight(BorderStyle.NONE);
        cellStyle.setBorderLeft(BorderStyle.NONE);
        cellStyle.setTopBorderColor(new XSSFColor(new java.awt.Color(199,199,199)));
        cellStyle.setBottomBorderColor(new XSSFColor(new java.awt.Color(199,199,199)));
    }

    private static List<CityChannelVO> getExportData() {
        List<CityChannelVO> results = new ArrayList<>();
        results.add(new CityChannelVO("杭州市", 1120, 901, 901, "80.45%", getExportStores()));
        results.add(new CityChannelVO("武汉市", 806, 592, 592, "73.45%", getExportStores()));
        results.add(new CityChannelVO("汇总", 1120 + 806, 901 + 592, 901 + 592, "69.70%", getExportLstStores()));
        return results;
    }

    private static List<StoreChannelVO> getExportStores() {
        List<StoreChannelVO> list = new ArrayList<>();
        list.add(new StoreChannelVO("BD专送", 820, getExportChannels()));
        list.add(new StoreChannelVO("BD人资", 200, getExportChannels()));
        list.add(new StoreChannelVO("KA(平台)", 50, getExportChannels()));
        list.add(new StoreChannelVO("C端运营", 200, getExportChannels()));
        return list;
    }

    private static List<ChannelVO> getExportChannels() {
        List<ChannelVO> list = new ArrayList<>();
        list.add(new ChannelVO(5, 5, 5, "80.45%"));
        list.add(new ChannelVO(15, 15, 15, "80.45%"));
        list.add(new ChannelVO(20, 20, 20, "80.45%"));
        list.add(new ChannelVO(7, 7, 5, "80.45%"));
        list.add(new ChannelVO(6, 12, 1, "80.45%"));
        return list;
    }

    private static List<StoreChannelVO> getExportLstStores() {
        List<StoreChannelVO> list = new ArrayList<>();
        list.add(new StoreChannelVO("-", 2170, getExportLastChannels()));
        return list;
    }

    private static List<ChannelVO> getExportLastChannels() {
        List<ChannelVO> list = new ArrayList<>();
        list.add(new ChannelVO(50, 50, 50, "69.70%"));
        list.add(new ChannelVO(150, 150, 150, "69.70%"));
        list.add(new ChannelVO(200, 200, 200, "69.70%"));
        list.add(new ChannelVO(70, 70, 50, "69.70%"));
        list.add(new ChannelVO(60, 120, 10, "69.70%"));
        return list;
    }

}
